﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace TKT.Lifter.CIM.Lifter.TibcoMsg.Lifter
{
    public class TraceUnit
    {
        public string UnitName { get; set; }
        public List<List<SVData>> Groups { get; set; }
    }

    public class SVData
    {
        public string SVID { get; set; }
        public string Value { get; set; }
    }

    public class TraceDataReport : MessageBody
    {
        public string MachineName { get; set; }
        public string TRID { get; set; }
        public string SMPLN { get; set; }
        public string STIME { get; set; }
        public List<TraceUnit> UnitList { get; set; }


        public TraceDataReport()
            : base("TraceData")
        { }

        public override System.Xml.Linq.XElement ToXElement()
        {
            XElement body = new XElement("Body");
            body.Add(new XElement("MACHINENAME", this.MachineName));
            body.Add(new XElement("TRID", this.TRID));
            body.Add(new XElement("SMPLN", this.SMPLN));
            body.Add(new XElement("STIME", this.STIME));

            XElement unitlist = new XElement("UNIT_LIST");
            if (this.UnitList != null)
            {
                foreach (var unit in this.UnitList)
                {
                    XElement unitXElement = new XElement("UNIT");

                    unitXElement.Add(new XElement("UNITNAME", unit.UnitName));

                    XElement grouplist = new XElement("GROUPLIST");
                    if (unit.Groups != null)
                    {
                        foreach (var svidlist in unit.Groups)
                        {
                            XElement svidlistXElement = new XElement("SVIDLIST");
                            if (svidlist != null)
                            {
                                foreach (var svdata in svidlist)
                                {
                                    XElement svdataXElement = new XElement("SVDATA");
                                    svdataXElement.Add(new XElement("SVID", svdata.SVID));
                                    svdataXElement.Add(new XElement("VALUE", svdata.Value));

                                    svidlistXElement.Add(svdataXElement);
                                }
                            }
                            grouplist.Add(svidlistXElement);
                        }
                    }
                    unitXElement.Add(grouplist);

                    unitlist.Add(unitXElement);
                }
            }

            body.Add(new XElement(unitlist));

            return body;
        }

        public override void Parse(System.Xml.Linq.XElement body)
        {
            this.MachineName = body.Element("MACHINENAME").Value;
            this.TRID = body.Element("TRID").Value;
            this.SMPLN = body.Element("SMPLN").Value;
            this.STIME = body.Element("STIME").Value;

            this.UnitList = new List<TraceUnit>();
            List<XElement> units = body.Element("UNIT_LIST").Elements("UNIT").ToList();
            foreach (var xelementUnit in units)
            {
                TraceUnit unit = new TraceUnit();
                unit.UnitName = xelementUnit.Element("UNITNAME").Value;

                unit.Groups = new List<List<SVData>>();
                List<XElement> groups = xelementUnit.Element("GROUPLIST").Elements("SVIDLIST").ToList();
                foreach (var xelmentGroup in groups)
                {
                    List<SVData> svDataList = new List<SVData>();
                    List<XElement> svidlist = xelmentGroup.Elements("SVDATA").ToList();
                    foreach (var xelementSVData in svidlist)
                    {
                        SVData svData = new SVData();
                        svData.SVID = xelementSVData.Element("SVID").Value;
                        svData.Value = xelementSVData.Element("VALUE").Value;
                        svDataList.Add(svData);
                    }
                    unit.Groups.Add(svDataList);
                }
                this.UnitList.Add(unit);
            }
        }
    }
}
